/**
 * @author Coande
 */
Modal = {
	show: function(modalSelector, callback) {
		this.modalSelector = modalSelector;
		var modalMask = $(".modal-mask");
		//只添加一层蒙版
		if(modalMask.length == 0) {
			$("body").append($("<div class='modal-mask'></div>"));
		}
		//先隐藏掉所有已打开
		$(".modal").hide();
		$(modalSelector).show();
		if(callback) {
			callback();
		}
	},
	hide: function(modalSelector, callback) {
		var selector = modalSelector || this.modalSelector;
		$(selector).hide();
		$(".modal-mask").remove();
		this.modalSelector = null;
		if(callback) {
			callback();
		}
	}
}

/**
 * 添加事件
 */
$(function() {

	//模态框打开事件
	$("body").on("click", "[data-toggle='modal']", function() {
		var selector = $(this).attr("data-target");
		Modal.show(selector);
	});

	//模态框关闭按钮事件
	$("body").on("click", ".modal .close", function() {
		var selector;
		if($(this).closest(".modal").attr("id")) {
			selector = "#" + $(this).closest(".modal").attr("id");
		} else {
			selector = ".modal";
		}
		Modal.hide(selector);
	});
});